package src.main.java.com.lyy.test;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;

public class CombineServer
{
    public static void main(String[] args)
    {
        try
        {
            ServerSocket serverSocket = new ServerSocket(12345); // 建立服务器，监听端口号为12345
            String saveDirectory;
            saveDirectory = "D:/Fileserver/"; // 文件保存目录
            Socket socket = serverSocket.accept(); // 等待客户端连接

            try
            {

                System.out.println("服务器等待客户端连接端口12345");
                System.out.println("客户端连接: " + socket);

                while(true)
                {
                    PrintStream out = new PrintStream(socket.getOutputStream());
                    BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

                    String ClientMassage;
                    String ServerMassage;
                    ClientMassage=in.readLine();//读取客户端发送的请求消息
                    String Massage = in.readLine();//读取客户端发送的具体消息

                    ServerMassage="已收到消息："+Massage+"。";//构造服务器的回复：“已收到消息：+传入消息”
                    out.println(ServerMassage);//发送客户端的消息
                    if(ClientMassage.equals("m")) //若客户端发送传输信息请求
                    {

                        ServerMassage="已收到消息："+Massage+"。";//构造服务器的回复：“已收到消息：+传入消息”
                        out.println(ServerMassage);//发送客户端的消息
                        System.out.println(ServerMassage);//打印服务器回复的消息
                    }
                    else if(ClientMassage.equals("f")) //若客户端发送传输文件请求
                    {
                        InputStream inputStream = socket.getInputStream();
                        Path directoryPath = Paths.get(saveDirectory);
                        if (!Files.exists(directoryPath)) // 如果保存文件的目录不存在，则创建
                        {
                            Files.createDirectories(directoryPath);
                        }

                        String extension = in.readLine(); // 读取文件扩展名
                        String fileName = UUID.randomUUID().toString() + extension; //生产
                        FileOutputStream fileOutputStream = new FileOutputStream(saveDirectory + fileName);

                        byte[] buffer = new byte[4096];
                        int bytesRead;
                        while ((bytesRead = inputStream.read(buffer)) != -1)
                        {
                            fileOutputStream.write(buffer, 0, bytesRead);
                        }
                        System.out.println("文件已被接收到: " + saveDirectory + fileName);
                        // fileOutputStream.close();//关闭文件流
                        //  inputStream.close();
                    }
                    if(ClientMassage.equals("Exit")) //若客户端请求退出
                    {
                        break;//结束循环，退出程序
                    }

                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            socket.close();//关闭socket
            serverSocket.close();//关闭服务器
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}